package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 请实现笔记本电脑使用USB鼠标、USB键盘的例子
 * 1. USB接口：包含打开设备、关闭设备功能
 * 2. 笔记本类：包含开机功能、关机功能、使用USB设备功能
 * 3. 鼠标类：实现USB接口，并具备点击功能
 * 4. 键盘类：实现USB接口，并具备输入功能
 * User: 石方旭
 * Date: 2022-04-08
 * Time: 22:30
 */
// USB接口
interface USB{
    void openDevice();
    void closeDevice();
}

//鼠标类

class Mouse implements USB{

    @Override
    public void openDevice() {
        System.out.println("打开鼠标！！！");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭鼠标！！！");
    }

    public void click() {
        System.out.println("点击鼠标！！！");
    }
}

// 键盘类，实现USB接口
class KeyBoard implements USB{

    @Override
    public void openDevice() {
        System.out.println("打开键盘！！！");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭键盘！！！");
    }

    public void inPut() {
        System.out.println("键盘输入！！！");
    }
}

//笔记本类：使用USB设备
class Computer {
    public void powerOn(){
        System.out.println("打开笔记本电脑");
    }
    public void powerOff(){
        System.out.println("关闭笔记本电脑");
    }
    public void useDevice(USB usb){
        usb.openDevice();
        //Mouse mouse = new Mouse();
        if(usb instanceof Mouse){
            Mouse mouse = (Mouse)usb;
            //Mouse mouse = new Mouse();
            mouse.click();
        }else if(usb instanceof KeyBoard){
            KeyBoard keyBoard = new KeyBoard();
            //KeyBoard keyBoard = (KeyBoard)usb;
            keyBoard.inPut();
        }
        usb.closeDevice();
    }
}

public class TestDemo1 {
    // 测试类：
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.powerOn();
        // 使用鼠标设备
        computer.useDevice(new Mouse());
        // 使用键盘设备
        computer.useDevice(new KeyBoard());
        computer.powerOff();
    }
}
